/**
 * 
 */
package org.gas.barapraja.handler;

import org.gas.barapraja.handler.validator.BindRequestValidator;
import org.gas.barapraja.proc.PduProcessor;

import com.logica.smpp.Data;
import com.logica.smpp.pdu.BindRequest;
import com.logica.smpp.pdu.BindResponse;
import com.logica.smpp.pdu.Response;

/**
 * @author gilang
 *
 */
public class BindRequestHandler extends GasHandlerAdapter {
	
	/**
	 * 
	 * @param pduProcessor
	 */
	public BindRequestHandler(PduProcessor pduProcessor){
		setPduProcessor(pduProcessor);
	}

	/* (non-Javadoc)
	 * @see org.gas.barapraja.handler.GasHandlerAdapter#handleRequest(com.logica.smpp.pdu.BindRequest)
	 */
	@Override
	public int handleRequest(BindRequest request) {
		int mode=request.getCommandId();
		int status = 0;
		if(mode==Data.BIND_RECEIVER||mode==Data.BIND_TRANSMITTER||mode==Data.BIND_TRANSCEIVER){
			BindResponse bindResponse=(BindResponse) request.getResponse();
			BindRequestValidator validator=new BindRequestValidator();
			status=validator.validate(request);
			if(status==Data.ESME_ROK){
				pduProcessor.serverResponse(bindResponse);
			}else{
				bindResponse.setCommandStatus(status);
				pduProcessor.serverResponse(bindResponse);
				pduProcessor.getSession().stop();
			}
		}else{
			if(request.canResponse()){
				status=Data.ESME_RINVBNDSTS;
				Response response=request.getResponse();
				pduProcessor.serverResponse(response);
			}else{
				
			}
			pduProcessor.getSession().stop();
		}
		return status;
	}
	
	

}


